package com.tql.view.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tql.view.pojo.Notice;
import com.tql.view.pojo.Scenery;
import com.tql.view.pojo.User;
import com.tql.view.service.NoticeService;
import com.tql.view.service.SceneryService;
import com.tql.view.util.DowLoadUtil;
import com.tql.view.util.JsonUtil;
import com.tql.view.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private SceneryService sceneryService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DowLoadUtil dowLoadUtil;
    /*  //输入关键字
    private static String antistop="美女";*/
    //编码关键字
    private static String encode;
    /*  private static int i=1;*/
    private static int name = 1;
    private static InputStream inputStream = null;
    private static FileOutputStream outputStream = null;
    //下载路径
    private static String path="E:\\view\\";
    //删除文件数量
    private static int c = 0;
    //下载图片的颜色
    private static String color = "0";
    //下载图片尺寸
    /*   private static String size = "9";*/
    @Autowired
    private NoticeService noticeService;
    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Value("${spring.mail.username}")
    private String sender;
    @RequestMapping("/toEmail")
    public String toEmail(){
        return "admin/email";
    }
    /*
    * 发送邮箱
    * */
    @RequestMapping("/email")
    public void email1(@RequestParam("topic") String topic,
                         @RequestParam("context") String context,
                         @RequestPart("face") MultipartFile face,
                         @RequestParam("email") String email) throws Exception, javax.mail.MessagingException {
        String originalFilename=face.getOriginalFilename();
        MimeMessage message=javaMailSender.createMimeMessage();
        //对message进行组装
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setSubject(topic);
        helper.setText(context);
        helper.addAttachment(originalFilename,face);
        helper.setSentDate(new Date());
        helper.setFrom(sender);
        helper.setTo(email);
        javaMailSender.send(message);
    }
    /*
    * 查看所有通知
    * */
    @RequestMapping("/getAllNotice")
    public String getAllNotice(Model model,
                               @RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "12")Integer size){
        List<Notice> list=noticeService.getAllNotice(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/notice";
    }
    /*
    * 根据通知id删除id
    * */
    @RequestMapping("/deleteNotice")
    public String deleteNotice(@RequestParam("id")String id){
        noticeService.deleteNotice(id);
        return "redirect:/admin/getAllNotice";
    }
    /*
    * 发布通知
    * */
    @RequestMapping("/pushNotice")
    public String pushNotice(@RequestParam("title")String title,
                             @RequestParam("content")String content, HttpSession session){
        User user= (User) session.getAttribute("visitor");
        Map<String,Object> params=new HashMap<>();
        String id= UUID.randomUUID()+"tql";
        Date createtime=new Date();
        params.put("id",id);
        params.put("title",title);
        params.put("content",content);
        params.put("pusher",user.getUsername());
        params.put("createtime",createtime);
        noticeService.insertNotice(params);
        return "redirect:/admin/getAllNotice";
    }
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        Notice notice=noticeService.getLastOne();
        return JsonUtil.objectToJson(notice);
    }
    /*
     * 查看所有通知
     * */
    @RequestMapping("/getAllNotices")
    public String getAllNotices(Model model,
                               @RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "12")Integer size){
        List<Notice> list=noticeService.getAllNotice(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "user/notice";
    }
    @RequestMapping("/welcome")
    public String welocme(){
        return "admin/welcome";
    }
    @RequestMapping("/tospider")
    public String toSpider(){
        return "admin/spider";
    }
    @RequestMapping("/spider")
    @ResponseBody
    public String Spider(@RequestParam("antistop")String antistop,
                         @RequestParam("page") String page,
                         @RequestParam("size") String size,
                         @RequestParam("type") String type) throws InterruptedException {

        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                String path1=path+type;
                try {
                    dowLoadUtil.commandLineDownload(antistop,encode,outputStream,inputStream,path1,size,color,name,c,Integer.parseInt(page));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        return "抓取成功";
    }
    //用户上传图片评审
    @RequestMapping("/mangerUserScenery")
    public String mangerUserScenery(Model model){
        List<Object> list=redisUtil.lGet("views",0,-1);
        String json=JsonUtil.objectToJson(list);
        List<Scenery> list1=JsonUtil.jsonToList(json,Scenery.class);
        model.addAttribute("pageInfo",list1);
        return "admin/showUserScenery";
    }
    //图片审批通过
    @RequestMapping("/passUserScenery")
    public String passUserScenery(@RequestParam("mytype")String mytype,
                                  @RequestParam("myname")String myname,
                                  @RequestParam("location")String location,
                                  @RequestParam("describes")String describes,
                                  @RequestParam("id") String id,
                                  @RequestParam("createtime") Date createtime,
                                  @RequestParam("pageview") int pageview
                                  ){
        //默认时间
        String mydate="2021-10-25 12:39:42";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(mydate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Scenery scenery=new Scenery(id,myname,location,describes,date,mytype,pageview,0);
        Map<String, Object> params = new HashMap<>();
        Date createTime = new Date();
        String ids = UUID.randomUUID().toString();
        params.put("id", ids);
        params.put("myname", myname);
        params.put("describes", describes);
        params.put("createtime", createTime);
        params.put("mytype",mytype);
        params.put("location",location);
        sceneryService.insertScenery(params);
        redisUtil.lRemove("views",1,scenery);
        return "redirect:/admin/mangerUserScenery";
    }
   @RequestMapping("/toNotice1")
   public String toNotice1(){

       return "admin/notice1";
   }
    //编辑通知
    @RequestMapping("/sendNotice")
    public String sendNotice(@RequestParam("content")String content){
        redisUtil.lpush("notice",content);
        return "redirect:/toView";
    }
    //获取通知
    @RequestMapping("/getNotice11")
    public String getNotice11(Model model){
        List<Object> list = redisUtil.lGet("notice", 0, -1);
        model.addAttribute("pageInfo",list);
        return "admin/showNotice";
    }
}
